package com.xiaoyi.common.log.logback;

import org.apache.commons.lang.StringUtils;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;

public class LineNoConverter extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent event) {
        try {
            StackTraceElement[] cda = event.getCallerData();
            if (cda != null && cda.length > 0) {
                StackTraceElement ste = cda[0];
                String className = ste.getClassName();
                if(StringUtils.isNotBlank(className)){
                    int lastIndexOfDot = StringUtils.lastIndexOf(className, ".");
                    if(lastIndexOfDot >= 0){
                        className = StringUtils.substring(className, lastIndexOfDot + 1);
                    }
                }
                return className + "." + ste.getMethodName() + ":" + ste.getLineNumber();
            }
            return "";
        } catch (Exception e) {
            return "GET LINE NO ERROR";
        }
    }

}
